package com.storm.lib.util;

import java.sql.Timestamp;
import java.util.Calendar;


import org.apache.log4j.Logger;

import com.storm.lib.type.DateType;


/**
 *
 * 类功能:时间类
 * @author johnny
 * @version 2010-11-3
 */
public class DateUtil {
	protected final static Logger logger = Logger.getLogger(DateUtil.class);
	/**
	 *
	 * 更新时间:2010-11-3, 作者:johnny
	 * 方法功能:得到当前时间
	 * @return
	 */
	public static Timestamp getCurrentTimeStamp() {
		Timestamp timestamp =new Timestamp(System.currentTimeMillis());
		return timestamp;
	}

	/**
	 *
	 * 更新时间:2010-11-3, 作者:johnny
	 * 方法功能:得到当前时间
	 * @return
	 */
	public static Timestamp getTimeStamp(long time) {
		Timestamp timestamp =new Timestamp(time);
		return timestamp;
	}

	/**
	 *
	 * 更新时间:2010-11-3, 作者:johnny
	 * 方法功能:得到增加的秒数
	 * @return
	 */
	public static long getAfterSeconds(int seconds) {
		long resultTime = addTime(System.currentTimeMillis(), DateType.TIME_TYPE_SECOND, seconds);
		return resultTime;
	}

	/**
	 *
	 * 更新时间:2010-11-3, 作者:johnny
	 * 方法功能:得到秒差
	 * @return
	 */
	public static long getCompareSeconds(Timestamp timestamp) {
		long delta = timestamp.getTime()-System.currentTimeMillis();
		delta=delta/1000;
		return Math.abs(delta);
	}

	/**
	 *
	 * 更新时间:2010-11-3, 作者:johnny
	 * 方法功能:得到秒差
	 * @return
	 */
	public static long getCompareSeconds(Long time) {
		long delta = time-System.currentTimeMillis();
		delta=delta/1000;
		return Math.abs(delta);
	}

	/**
	 *
	 * 更新时间:2010-11-3, 作者:johnny
	 * 方法功能:得到秒差
	 * @return
	 */
	public static long getCompareMinute(Long time) {
		long delta=time/(1000*60);
		return Math.abs(delta);
	}

	/**
	 *
	 * 更新时间:2010-11-3, 作者:johnny
	 * 方法功能:得到代表“0”的时间戳
	 * @return
	 */
	public static Timestamp getZeroTimeStamp() {
		Timestamp timestamp =new Timestamp(0);
		timestamp.setTime(0);
		logger.debug(timestamp.toLocaleString());
		return timestamp;
	}

	/**
	 *
	 * 更新时间:2010-11-14, 作者:johnny
	 * 方法功能: 判断传入时间是否在当前时间指定的过去时间段之内
	 * @param time
	 * @param timeType
	 * @param num
	 * @return
	 */
	public static boolean inPassed(long time, int timeType,int num) {

		long passedStamp = time;
		long currentStamp = System.currentTimeMillis();
		long passedDeltaStamp = currentStamp - passedStamp;
		if(passedDeltaStamp<0){
			return false;
		}
		if(passedDeltaStamp==0){
			return true;
		}
		if(timeType == DateType.TIME_TYPE_HOUR){
			if(passedDeltaStamp<=1l*num*3600*1000){
				return true;
			}
		}
		return false;
	}

	/**
	 *
	 * 方法功能:增加时间
	 * 更新时间:2010-11-25, 作者:johnny
	 * @param time
	 * @param timeType
	 * @param num
	 * @return
	 */
	public static long addTime(long time, int timeType,int num) {
//		long targetTime = time;
		long timeCount= 1000*getSecondsCount(timeType,num);
		return time+timeCount;
	}

	/**
	 *
	 * 方法功能:减少时间
	 * 更新时间:2010-11-25, 作者:johnny
	 * @param time
	 * @param timeType
	 * @param num
	 * @return
	 */
	public static long reduceTime(long time, int timeType,int num) {
//		long targetTime = time;
		long timeCount= getSecondsCount(timeType,num);
		return time-timeCount;
	}


	/**
	 * 方法功能:计算时间
	 * 更新时间:2010-11-25, 作者:johnny
	 * @param timeType
	 * @param num
	 * @return
	 */
	public static long getSecondsCount(int timeType, int num) {
		if(timeType==DateType.TIME_TYPE_SECOND){
			return num;
		}
		if(timeType==DateType.TIME_TYPE_MINUTE){
			return 1l*num*60;
		}
		if(timeType==DateType.TIME_TYPE_HOUR){
			return 1l*num*3600;
		}
		return 0;
	}

	/**
	 * 方法功能:计算时间
	 * 更新时间:2010-11-25, 作者:johnny
	 * @param timeType
	 * @param num
	 * @return
	 */
	public static long getMSCount(int timeType, int num) {
		if(timeType==DateType.TIME_TYPE_MS){
			return num;
		}
		if(timeType==DateType.TIME_TYPE_SECOND){
			return 1l*num*1000;
		}
		if(timeType==DateType.TIME_TYPE_MINUTE){
			return 1l*num*60*1000;
		}
		if(timeType==DateType.TIME_TYPE_HOUR){
			return 1l*num*3600*1000;
		}
		return 0;
	}

	/**
	 * 功能:计算两个时间的毫秒差
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static long getMSCount(Timestamp time1, Timestamp time2) {
		if(null!=time1 && null!=time2){
			return time1.getTime() - time2.getTime();
		}
		return 0;
	}

	/**
	 * 方法功能:获取文本
	 * 更新时间:2010-11-30, 作者:johnny
	 * @param time
	 * @return
	 */
	public static String getText(long time) {
		return new Timestamp(time).toLocaleString();
	}

	/**
	 * 方法功能:判断目标时间是否和当前小时一致
	 * 更新时间:2011-1-17, 作者:johnny
	 * @param targetTime
	 * @return
	 */
	public static boolean inCurrentHour(Timestamp targetTime) {
		Calendar nowTime = Calendar.getInstance();
		nowTime.set(Calendar.MILLISECOND, 0);
		nowTime.set(Calendar.SECOND, 0);
		nowTime.set(Calendar.MINUTE, 0);

		Calendar targetCalendar = Calendar.getInstance();
		targetCalendar.setTime(targetTime);
		targetCalendar.set(Calendar.MILLISECOND, 0);
		targetCalendar.set(Calendar.SECOND, 0);
		targetCalendar.set(Calendar.MINUTE, 0);

		if(nowTime.equals(targetCalendar)){
			return true;
		}
		return false;
	}


	public static void main(String[] args) {

		while(true){
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.MINUTE, 41);
			logger.debug(calendar.getTime().toLocaleString()+"-->>");
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			logger.debug(calendar.getTime().toLocaleString()+"\n");
			logger.debug(calendar.get(Calendar.HOUR_OF_DAY));
			if(inCurrentHour(new Timestamp(calendar.getTime().getTime()))){
				logger.debug("在这个小时是一样的");
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				ExceptionUtil.processPrintException(e);
			}
		}

	}

	/**
	 * 方法功能:获得下个小时的整点
	 * 更新时间:2011-1-19, 作者:johnny
	 * @return
	 */
	public static Timestamp getNextHourTime() {
		Calendar nowTime = Calendar.getInstance();
		nowTime.set(Calendar.MILLISECOND, 0);
		nowTime.set(Calendar.SECOND, 0);
		nowTime.set(Calendar.MINUTE, 0);
		nowTime.add(Calendar.HOUR_OF_DAY, 1);
		return new Timestamp(nowTime.getTime().getTime());
	}

	/**
	 * 方法功能:是过去的时间
	 * 更新时间:2011-1-20, 作者:johnny
	 * @param endTime
	 * @return
	 */
	public static boolean inPass(long time) {
		if(time<System.currentTimeMillis()){
			return true;
		}
		return false;
	}

	/**
	 * 计算两个时间的分钟差
	 * @param t1
	 * @param t2
	 * @return
	 */
	public static long getMinute(Timestamp t1, Timestamp t2) {
		long i = t2.getTime() - t1.getTime();
		long j = 1000 * 60;
		long k = i / j;
		return k;
	}

	/**
	 * 检查日期格式是否正确
	 * @param str
	 * @return
	 */
	public static String checkTimeValue(String str){
		String err = "";
		try {
			Timestamp.valueOf(str);
		} catch (Exception e) {
			err="日期格式错误";
		}
		return err;
	}

	/**
	 * 获得当前小时
	 * @return
	 */
	public static int getCurHour() {
		Calendar calandar = Calendar.getInstance();
		int hour = calandar.get(Calendar.HOUR_OF_DAY);
		return hour;
	}

	/**
	 * 判断两个时间时间是否是同一天
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static boolean isSameDay(Timestamp time1,Timestamp time2){
		if(null!=time1 && null!=time2){
			Calendar c1 = Calendar.getInstance();
			c1.setTime(time1);
			int y1 = c1.get(Calendar.YEAR);
			int m1 = c1.get(Calendar.MONTH);
			int d1 = c1.get(Calendar.DATE);

			Calendar c2 = Calendar.getInstance();
			c1.setTime(time2);
			int y2 = c2.get(Calendar.YEAR);
			int m2 = c2.get(Calendar.MONTH);
			int d2 = c2.get(Calendar.DATE);

			if(y1==y2 && m1==m2 && d1==d2){
				return true;
			}
		}
		return false;

	}
	/**
	 * 当前时间与指定时间是否超过了指定天数
	 * @param time
	 * @param day
	 * @return
	 */
	public static boolean isPassDay(Timestamp time,int day) {
		if(System.currentTimeMillis()-time.getTime()>=day*24*60*60*1000){
			return true;
		}
		return false;
	}
	
	public static long getCurrentTime(){
		return System.currentTimeMillis();
	}

//	public static void main(String[] args) {
//	long tt=System.currentTimeMillis();
//	inPassed(tt, DateType.TIME_TYPE_HOUR, 2);
//}


}
